<?php
/**
 * 消息
 * @return
 * @author yuncran@126.com
 */

namespace app\services;

use app\model\MessageTemplate;
use app\model\MessageTemplateModel;
use app\model\SystemMessageModel;

class MessageService
{

    /**
     * 获取固定模版
     * @return void
     * @author yuncran@126.com
     */
    public static function getTemplate($template_code, $user_message_data)
    {
        $messageTemplate = MessageTemplateModel::where(['code' => $template_code])->find();
        if (empty($messageTemplate)) {
            return echoShutdown(lang('未找到相关消息模版'), 2001);
        }
        if ('100001' == $messageTemplate['code']) {
            $message = $messageTemplate['describe'];
            $default_message = $messageTemplate['example'];
        } elseif ('100002' == $messageTemplate['code']) {
            $message = $messageTemplate['describe'];
            $default_message = $messageTemplate['example'];
        } elseif ('100003' == $messageTemplate['code']) {
            $message = $messageTemplate['describe'];
            $default_message = $messageTemplate['example'];
        } elseif ('100004' == $messageTemplate['code']) {
            $message = $messageTemplate['describe'];
            $default_message = $messageTemplate['example'];
        } elseif ('100005' == $messageTemplate['code']) {
            $message = $messageTemplate['describe'];
            $default_message = $messageTemplate['example'];
        }
        $result = self::messageMutate($template_code, $message, $user_message_data, $default_message);
        return $result;
    }

    //消息模板数据转换
    private static function messageMutate($template_code, $message, $user_message_data, $default_message)
    {
        if ('100001' == $template_code) {
            $message = !empty($message) ? $message : $default_message;
            $username = $user_message_data['username']; //会员名称
            $now_money = $user_message_data['money'];   //余额
            $replacements = array('{name}' => $username, '{money}' => $now_money);
            $result_message = str_replace(array_keys($replacements), array_values($replacements), $message);
        } elseif ('100002' == $template_code) {
            $message = !empty($message) ? $message : $default_message;
            $username = $user_message_data['username'];   //会员名称
            $gift_name = $user_message_data['gift_name']; //礼物名称
            $now_money = $user_message_data['money'];     //礼物金额
            $replacements = array('{name}' => '<span style="color:#0a84ff">' . $username . '</span>', '{gift_name}' => '<span style="color:#0a84ff">' . $gift_name . '</span>', '{money}' => $now_money);
            $result_message = str_replace(array_keys($replacements), array_values($replacements), $message);
        } elseif ('100003' == $template_code) {
            $message = !empty($message) ? $message : $default_message;
            $username = $user_message_data['username'];   //会员名称
            $trends_name = $user_message_data['trends_name']; //动态标题名称
            $str = '<span style="color:#0a84ff;white-space: nowrap; overflow: hidden;text-overflow: ellipsis;">' . $trends_name . '</span>';
            $replacements = array('{name}' => '<span style="color:#0a84ff">' . $username . '</span>', '{trends_name}' => $str);
            $result_message = str_replace(array_keys($replacements), array_values($replacements), $message);
        } elseif ('100004' == $template_code) {
            $message = !empty($message) ? $message : $default_message;
            $username = $user_message_data['username'];   //会员名称
            $replacements = array('{name}' => $username);
            $result_message = str_replace(array_keys($replacements), array_values($replacements), $message);
        } elseif ('100005' == $template_code) {
            $message = !empty($message) ? $message : $default_message;
            $username = $user_message_data['username'];   //会员名称
            $now_money = $user_message_data['money'];     //礼物金额
            $replacements = array('{name}' => '<span style="color:#0a84ff">' . $username . '</span>', '{money}' => $now_money);
            $result_message = str_replace(array_keys($replacements), array_values($replacements), $message);
        }
        return $result_message;
    }


    public static function getValue($template_code, $user_message_data)
    {
        $messageTemplate = MessageTemplateModel::where(['code' => $template_code])->find();
        if ('100001' == $template_code) {
            $username = $user_message_data['username']; //会员名称
            $now_money = $user_message_data['money'];   //余额
            $replacements = array('{name}' => $username, '{money}' => $now_money);
            $data = [
                'result_message' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe']),
                'result_message_tw' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_tw']),
                'result_message_us' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_us']),
                'result_message_jp' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_jp']),
                'result_message_kr' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_kr']),
            ];
        } elseif ('100002' == $template_code) {

            $username = $user_message_data['username'];   //会员名称
            $gift_name = $user_message_data['gift_name']; //礼物名称
            $now_money = $user_message_data['money'];     //礼物金额
            $replacements = array('{name}' => '<span style="color:#0a84ff">' . $username . '</span>', '{gift_name}' => '<span style="color:#0a84ff">' . $gift_name . '</span>', '{money}' => $now_money);
            $data = [
                'result_message' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe']),
                'result_message_tw' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_tw']),
                'result_message_us' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_us']),
                'result_message_jp' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_jp']),
                'result_message_kr' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_kr']),
            ];
        } elseif ('100003' == $template_code) {
            $username = $user_message_data['username'];   //会员名称
            $trends_name = $user_message_data['trends_name']; //动态标题名称
            $str = '<span style="color:#0a84ff;white-space: nowrap; overflow: hidden;text-overflow: ellipsis;">' . $trends_name . '</span>';
            $replacements = array('{name}' => '<span style="color:#0a84ff">' . $username . '</span>', '{trends_name}' => $str);
            $data = [
                'result_message' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe']),
                'result_message_tw' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_tw']),
                'result_message_us' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_us']),
                'result_message_jp' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_jp']),
                'result_message_kr' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_kr']),
            ];
        } elseif ('100004' == $template_code) {
            $username = $user_message_data['username'];     // 会员名称
            $replacements = array('{name}' => $username);
            $data = [
                'result_message' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe']),
                'result_message_tw' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_tw']),
                'result_message_us' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_us']),
                'result_message_jp' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_jp']),
                'result_message_kr' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_kr']),
            ];
        } elseif ('100005' == $template_code) {
            $username = $user_message_data['username'];     // 会员名称
            $replacements = array('{name}' => $username);
            $data = [
                'result_message' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe']),
                'result_message_tw' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_tw']),
                'result_message_us' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_us']),
                'result_message_jp' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_jp']),
                'result_message_kr' => str_replace(array_keys($replacements), array_values($replacements), $messageTemplate['describe_kr']),
            ];
        }
        return $data;

    }

    /**
     * 加入系统消息记录
     * @param $template_code
     * @param $user_message_data
     * @param $user_id
     * @param $type
     * @return \app\model\SystemMessage|\think\Model
     * @author yuncran@126.com
     */
    public function messageMutateAdd($template_code, $user_message_data, $user_id, $type)
    {
        return \app\model\SystemMessageModel::create([
            'user_id' => $user_id,
            'type' => $type,
            'describe' => self::getTemplate($template_code, $user_message_data),
            'content' => self::getTemplate($template_code, $user_message_data),
        ]);
    }


    /**
     * 系统消息
     * @return void
     * @author yuncran@126.com
     */
    public static function save()
    {
//        fa_system_message
        SystemMessageModel::where()->save(['status' => '1', 'update_time' => date('Y-m-d H:i:s', time())]);
    }


}